﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Topology;

using System.Drawing;

using System.Drawing.Drawing2D;

using DotSpatial.Data;

using Point = System.Drawing.Point;



namespace FXnaGis
{

    [Serializable]
    /// <summary>
    /// 提供最基础的多边形支持
    /// </summary>
    public class FPolyShape:FShape
    {


        protected FPolyShape(string id)
            : base(id)
        {

           
 

        }


        public FPolyShape(string id, List<Coordinate> coords):base(id)
        {
            

            this._polygon = new Polygon(coords.CloneList<Coordinate>());

          

 

        }


        protected  Polygon _polygon;


        /// <summary>
        /// 对外的主要绘制属性
        /// </summary>
        public Polygon Polygon
        {
            get { return _polygon; }
            set {
                
                _polygon = value;

             

            }
        }


        public override void Move(double xOffset, double yOffset, double zOffset)
        {
            foreach (Coordinate c in this._polygon.Coordinates)
            {
                c.X += xOffset;
                c.Y += yOffset;
                c.Z += zOffset;


            }

            this._polygon.UpdateEnvelope();

        }

        public override void Draw(FMapArgs args)
        {
          

            PolyStyle polyStyle = this.Parent.PolyStyle;

            //if (style == null)
            //{
            //    polyStyle = PolyStyle.DefaultStyle;


            //}
            //else
            //{
            //     polyStyle = style as PolyStyle;
 
            //}

           



            //得到边界切割算法


            Rectangle clipRect = ComputeClippingRectangle(args);
            Extent drawExtents = args.PixelToProj(clipRect);
            SoutherlandHodgman shClip = new SoutherlandHodgman(clipRect);





           

            List<Point> pts = new List<Point>();




            //首先循环外边

            foreach (DotSpatial.Topology.Coordinate c in this.Polygon.Coordinates)
            {
                pts.Add(args.ProjToPixel(c));


            }


            if (pts.Count < 2)
            {

                return;
            }


            //先用一个简易的方法代替，之后再做修改



            //if (null != shClip)
            //{
            //    pts = shClip.Clip(pts);
            //}






            //之后是想办法去掉内嵌的洞,我们暂时不支持这一操作

            for (int i = 0; i < this.Polygon.Holes.Length; i++)
            {


            }




            GraphicsPath path = new GraphicsPath();


            path.StartFigure();
            Point[] pointArray = pts.ToArray();
            path.AddLines(pointArray);




            //这里需要闭合

            path.CloseFigure();


            path.StartFigure();




            polyStyle.DrawPath(args.Device, path, 1);

            polyStyle.FillPath(args.Device, path);


            path.Dispose();

        }


      


       
        public override Extent Extent
        {
            get
            {
                return new DotSpatial.Data.Extent(this._polygon.Envelope);
            }
        }



        public override bool Intersects(Envelope envelope)
        {
            return this._polygon.Intersects(envelope);
        }



        private static Rectangle ComputeClippingRectangle(FMapArgs args)
        {
            const int maxSymbologyFuzz = 50;
            var clipRect = new Rectangle(args.ImageRectangle.Location.X, args.ImageRectangle.Location.Y, args.ImageRectangle.Width, args.ImageRectangle.Height);
            clipRect.Inflate(maxSymbologyFuzz, maxSymbologyFuzz);
            return clipRect;
        }


        /// <summary>
        /// Appends the specified polygon to the graphics path.
        /// </summary>
        private static void BuildPolygon(double[] vertices, FShape shape, GraphicsPath borderPath, FMapArgs args, SoutherlandHodgman shClip)
        {
            //double minX = args.MinX;
            //double maxY = args.MaxY;
            //double dx = args.Dx;
            //double dy = args.Dy;

            //for (int prt = 0; prt < shpx.Parts.Count; prt++)
            //{
            //    PartRange prtx = shpx.Parts[prt];
            //    int start = prtx.StartIndex;
            //    int end = prtx.EndIndex;
            //    List<double[]> points = new List<double[]>();

            //    for (int i = start; i <= end; i++)
            //    {
            //        double[] pt = new double[2];
            //        pt[X] = (vertices[i * 2] - minX) * dx;
            //        pt[Y] = (maxY - vertices[i * 2 + 1]) * dy;



            //        points.Add(pt);
            //    }
            //    if (null != shClip)
            //    {
            //        points = shClip.Clip(points);
            //    }
            //    List<Point> intPoints = DuplicationPreventer.Clean(points);
            //    if (intPoints.Count < 2)
            //    {
            //        points.Clear();
            //        continue;
            //    }

            //    //Would be nice to figure out how to get rid of this lock
            //    lock (lock1)
            //    {
            //        borderPath.StartFigure();
            //        Point[] pointArray = intPoints.ToArray();
            //        borderPath.AddLines(pointArray);
            //    }
            //    points.Clear();
            //}
        }



    }
}
